package com.jiazimall.commodity.controller;

import com.jiazimall.commodity.pojo.CategoryEntity;
import com.jiazimall.commodity.service.CategoryService;
import com.wujiahao.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public R list() {
        List<CategoryEntity> entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }

    @RequestMapping("/delete")
    public R deleteCategory(@RequestBody Long[] catId) {
        return categoryService.removeMenusByIds(Arrays.asList(catId));
    }

    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity categoryEntity) {
        categoryService.save(categoryEntity);
        return R.ok();
    }

    //用于修改菜单时回显节点数据
    @RequestMapping("/info/{catID}")
    public R info(@PathVariable Long catID) {
        CategoryEntity categoryEntity = categoryService.getById(catID);
        return R.ok().put("category", categoryEntity);
    }

    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity categoryEntity) {
        categoryService.updateDetail(categoryEntity);
        return R.ok();
    }
    @RequestMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] categoryEntitys) {
        categoryService.updateBatchById(Arrays.asList(categoryEntitys));
        return R.ok();
    }
}
